package com.ld.utils;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationAction;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.IconLoader;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

public class CommonUtils {

    final static String NOTIFICATION_GROUP_ID = "probie_notification";
    final static String NOTIFICATION_DEFAULT_TITLE = "Probie Translation";

    static volatile ExecutorService executorService = Executors.newFixedThreadPool(1);


    static Notification notification;

    public final static Icon defaultIcon = IconLoader.getIcon("/icons/probie.png");

    public static boolean isMatch(String pat, String v) {
        Pattern pattern = Pattern.compile(pat);
        int length = v.length();
        if (length == 0) return false;
        if (length == 1){
            return pattern.matcher(v).matches();
        }
        for (int i=0;i<length;i++){
            String s = String.valueOf(v.charAt(i));
            if (s.trim().length() <= 0) continue;
            if (!pattern.matcher(s).matches()){
                return false;
            }
        }
        return true;
    }

    public static Optional<String> englishWordSegmentation(String enWord){
        try {
            Pattern compile = Pattern.compile("[a-zA-Z]");
            Pattern capital = Pattern.compile("[A-Z]");
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < enWord.length(); i++) {
                String v = String.valueOf(enWord.charAt(i));
                if (!compile.matcher(v).matches()){
                    String substring = sb.substring(sb.length() - 1);
                    if (!StringUtils.isBlank(substring)){
                        sb.append(" ");
                    }
                    continue;
                }
                if (capital.matcher(v).matches()){//capital
                    String substring = sb.substring(sb.length() - 1);
                    if (!StringUtils.isBlank(substring)){
                        sb.append(" ");
                    }
                    sb.append(v);
                }else {
                    sb.append(v);
                }
            }
            return Optional.ofNullable(sb.toString());
        }catch (Exception e){
            e.printStackTrace();
            return Optional.empty();
        }
    }

    public static void notification(String content, NotificationAction... notificationActions){
        if (notification != null){
            notification.hideBalloon();
        }
        notification = new Notification(NOTIFICATION_GROUP_ID, CommonUtils.defaultIcon, CommonUtils.NOTIFICATION_DEFAULT_TITLE, "", content, NotificationType.INFORMATION, null);
        if (notificationActions.length > 0){
            for (int i = 0; i < notificationActions.length; i++) {
                notification.addAction(notificationActions[i]);
            }
        }
        Notifications.Bus.notify(notification);
    }

    public static void notification(String title ,String content, NotificationAction... notificationActions){
        if (notification != null){
            notification.hideBalloon();
        }
        notification = new Notification(NOTIFICATION_GROUP_ID, CommonUtils.defaultIcon,title, "", content, NotificationType.INFORMATION, null);
        if (notificationActions.length > 0){
            for (int i = 0; i < notificationActions.length; i++) {
                notification.addAction(notificationActions[i]);
            }
        }
        Notifications.Bus.notify(notification);
    }

    public static void run(Runnable runnable){
        ApplicationManager.getApplication().executeOnPooledThread(runnable);
    }
}
